/* The MIT License:

Copyright (c) 2015 Ivan Gagis <igagis@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

// Home page: http://stob-java.googlecode.com


package org.stobml;

import java.io.IOException;
import java.io.InputStream;


/**
 *
 * @author ivan
 */
public class STOBParser {
	
	private enum EState{
		IDLE,
		UNQUOTED_STRING,
		QUOTED_STRING,
		SINGLE_LINE_COMMENT,
		MULTILINE_COMMENT
	}

	public interface Listener{
		public void onStringParsed(String str);
		public void onChildrenParseStarted();
		public void onChildrenParseFinished();
	}
	
	public static class FormatException extends Exception{
		
	}
	
	public static void Parse(InputStream is, Listener listener) throws IOException, FormatException{
		char prevChar = '\0';
	
		EState state = EState.IDLE;

		boolean stringHasJustBeenParsed = false;

		StringBuilder curString = new StringBuilder();

		for(int b = is.read(); b >= 0; b = is.read()){
			char c = (char)b;

			switch (state) {
			case IDLE:
				switch (c) {
				case '/':
					if(prevChar == '/') {
						state = EState.SINGLE_LINE_COMMENT;
					}
					//do not append the character here yet
					break;
				case '*':
					if (prevChar == '/') {
						state = EState.MULTILINE_COMMENT;
					} else {
						curString.append (c);
					}
					break;
				case ' ':
				case '\n':
				case '\r':
				case '\t':
					break;
				case '{':
					if (!stringHasJustBeenParsed) {
						listener.onStringParsed ("");
					}
					stringHasJustBeenParsed = false;
					listener.onChildrenParseStarted ();
					break;
				case '}':
					stringHasJustBeenParsed = false;
					listener.onChildrenParseFinished ();
					break;
				case '"':
					state = EState.QUOTED_STRING;
					break;
				default:
					if (prevChar == '/') {
						curString.append ('/');
					}
					curString.append (c);
					state = EState.UNQUOTED_STRING;
					break;
				}
				break;

			case UNQUOTED_STRING:
				switch (c) {
				case '/':
					if (prevChar == '/') {
						state = EState.SINGLE_LINE_COMMENT;
					}
					//do not append the character here yet
					break;
				case '*':
					if (prevChar == '/') {
						state = EState.MULTILINE_COMMENT;
					} else {
						curString.append (c);
					}
					break;
                case '"':
                    listener.onStringParsed(curString.toString());
                    curString = new StringBuilder();
                    state = EState.QUOTED_STRING;
                    stringHasJustBeenParsed = true;
                    break;
				case ' ':
				case '\r':
				case '\n':
				case '\t':
					listener.onStringParsed (curString.toString ());
					curString = new StringBuilder ();
					state = EState.IDLE;
					stringHasJustBeenParsed = true;
					break;
				case '{':
					listener.onStringParsed (curString.toString ());
					curString = new StringBuilder ();
					state = EState.IDLE;
					listener.onChildrenParseStarted ();
					stringHasJustBeenParsed = false;
					break;
				case '}':
					listener.onStringParsed (curString.toString ());
					curString = new StringBuilder ();
					state = EState.IDLE;
					stringHasJustBeenParsed = false;
					listener.onChildrenParseFinished ();
					break;
				default:
					if (prevChar == '/') {
						curString.append ('/');
					}
					curString.append (c);
					break;
				}
				break;

			case QUOTED_STRING:
				switch (c) {
				case '"':
					if (prevChar == '\\') {
						curString.append ('\"');
					} else {
						listener.onStringParsed (curString.toString ());
						curString = new StringBuilder ();
						state = EState.IDLE;
						stringHasJustBeenParsed = true;
					}
					break;
				case '\\':
					if (prevChar == '\\') {
						curString.append ('\\');
						c = '\0';//this is to make new prevChar not to be '\' next cycle
					}
					break;
				case '\r':
				case '\n':
				case '\t':
					//ignore
					break;
				default:
					if (prevChar == '\\') {//if escape sequence
						switch (c) {
						case 'r':
							curString.append ('\r');
							break;
						case 'n':
							curString.append ('\n');
							break;
						case 't':
							curString.append ('\t');
							break;
						default:
							//ignore
							break;
						}
					}else{
						curString.append(c);
					}
					break;
				}
				break;
			case SINGLE_LINE_COMMENT:
				if (c == '\n') {
					state = EState.IDLE;
				}
				break;
			case MULTILINE_COMMENT:
				if (c == '/' && prevChar == '*') {
					state = EState.IDLE;
					c = '\0';//this is to make new prevChar not to be '/' next cycle
				}
				break;
			default:
				//Assert(false)
				break;
			}

			prevChar = c;
		}//~while(not end of stream)

		switch (state) {
		case IDLE:
			//do nothing
			break;
		case UNQUOTED_STRING:
			listener.onStringParsed (curString.toString ());
			break;
		default:
			throw new FormatException();
		}
	}
}
